package cn.dnui_lf_705.dnui_16110100705.controller;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.ResultBean;
import cn.dnui_lf_705.dnui_16110100705.listener.OnRetrofitListener;
import cn.dnui_lf_705.dnui_16110100705.model.ShopModel;


public class PurchaseActivity extends BaseActivity implements OnRetrofitListener<ResultBean> {
    TextView foodname,price,num;
    EditText address,time;
    Button tijiao;
    ImageView jia,jian;
    String user_id,address1,foodname1;
    int food_id;
    Double price1;
    SharedPreferences sharedPreferences;
    int i=1;
    double pp=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);
        food_id = getIntent().getIntExtra("food_id",11);
        foodname1 = getIntent().getStringExtra("foodname");
        address1 = getIntent().getStringExtra("address");
        price1 = getIntent().getDoubleExtra("price",0.00);
        sharedPreferences = getSharedPreferences("yht",Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("user_id","");
        initViews();
        initData();
        initEvents();
        if (setEnableSliding()) {
            ArActSlidLayout rootView = new ArActSlidLayout(this);
            rootView.bindActivity(this);//绑定需要窗口的布局：
        }
    }
    protected boolean setEnableSliding() {//默认返回是需要的，只需继承的时候重写次代码 true : 需要；false :不需要；
        return true;
    }
    @Override
    public void onsuccess(ResultBean resultBean, int flag) {
        if(resultBean.getSuccess().equals("1")){
            showToast("购买成功");
            finish();
        }
        else{
            showToast("购买失败");
        }
    }

    @Override
    public void onFailure(String string) {
        showToast("网络错误");
    }
    @Override
    void initViews() {
        foodname = findViewById(R.id.foodname);
        num = findViewById(R.id.geshu);
        price = findViewById(R.id.jiage);
        address = findViewById(R.id.songhuodizhi);
        time = findViewById(R.id.songhuoshijian);
        tijiao = findViewById(R.id.tijiao);
        jia = findViewById(R.id.add_button1);
        jian = findViewById(R.id.add_button);
    }

    @Override
    void initEvents() {
        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i++;
                pp = price1*i;
                num.setText(String.valueOf(i));
                price.setText(String.valueOf(pp));

            }
        });
        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i>1)
                {
                    i--;
                    pp = price1*i;
                    num.setText(String.valueOf(i));
                    price.setText(String.valueOf(pp));
                }
                else
                {
                    showToast("商品至少为一个");
                }
            }
        });
        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ti();
            }
        });

    }
   void ti(){
       ShopModel shopModel = new ShopModel();
       shopModel.insertOrder(user_id,String.valueOf(food_id),String.valueOf(i),String.valueOf(pp),time.getText().toString().trim(),address.getText().toString().trim(),this);
    }

    @Override
    void initData() {
            foodname.setText(foodname1);
            num.setText(String.valueOf(i));
            address.setText(address1);

            time.setText("预计送达时间："+getTime());
            price.setText(String.valueOf(price1));
    }
    String getTime(){

        long currentTime = System.currentTimeMillis() + 30 * 60 * 1000+28800;
        Date d = new Date(currentTime);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(d);
    }
}
